﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class PositionTypeDAL : IPositionTypeDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public PositionTypeDAL()
        {

        }
        public DataSet LoadPositionTypes(Nullable<int> Sequence, Nullable<int> EmployeeTypes)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@EmployeeTypes", EmployeeTypes);
                dbManager.AddParameters(2, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(3, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = "SELECT pt.POS_TYPE_SEQ, pt.POS_TYPE_NAME, pt.POS_TYPE_NAME_SHORT"
                        + ", pt.POS_TYPE_ENG, pt.POS_TYPE_ENG_SHORT, pt.REMARKS"
                        + ", pt.EMP_TYPE_SEQ, et.EMP_TYPE_NAME"
                        + ", pt.RECORD_STATUS, pt.CREATED_BY, pt.UPDATED_BY"
                    + " FROM HRM_MP_POSITION_TYPES pt"
                        + " LEFT JOIN HRM_PS_EMPLOYEE_TYPES et ON pt.EMP_TYPE_SEQ = et.EMP_TYPE_SEQ"
                    + " WHERE pt.RECORD_STATUS = @ActiveRecord"
                    + (Sequence.HasValue ? " AND pt.POS_TYPE_SEQ = @Sequence" : "")
                    + (EmployeeTypes.HasValue ? " AND pt.EMP_TYPE_SEQ = @EmployeeTypes" : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

                
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return ds;
        }   
        public IList<PositionTypeInfo> ListPositionTypes(Nullable<int> Seq, Nullable<int> EmployeeTypes)
        {
            string strSQL;

            List<PositionTypeInfo> infoList = new List<PositionTypeInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@Seq", Seq);
                dbManager.AddParameters(1, "@EmployeeTypes", EmployeeTypes);
                dbManager.AddParameters(2, "@ActiveRecord", SystemInfo._ActiveRecordStatus);

                strSQL = "SELECT POS_TYPE_SEQ, POS_TYPE_NAME, POS_TYPE_NAME_SHORT"
                        + ", POS_TYPE_ENG, POS_TYPE_ENG_SHORT, REMARKS, CREATED_BY, UPDATED_BY"
                        + ", EMP_TYPE_SEQ, RECORD_STATUS"
                    + " FROM HRM_MP_POSITION_TYPES"
                    + " WHERE RECORD_STATUS = @ActiveRecord"
                    + (Seq.HasValue ? " AND POS_TYPE_SEQ = @Seq" : "")
                    + (EmployeeTypes.HasValue? " AND EMP_TYPE_SEQ = @EmployeeTypes" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    EmployeeTypeDAL emptypeDAL = new EmployeeTypeDAL();
                    EmployeeTypeInfo emptypeInfo = new EmployeeTypeInfo();
                    if (!rdr.IsDBNull(8))
                    {
                        IList<EmployeeTypeInfo> emptypeList = emptypeDAL.ListEmployeeTypes(new EmployeeTypeInfo(rdr.IsDBNull(8) ? 0 : rdr.GetInt32(8)));
                        emptypeInfo = emptypeList.Count > 0 ? emptypeList[0] : null;
                    }

                    PositionTypeInfo Info = new PositionTypeInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                            , emptypeInfo
                            , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                            , rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6)
                            , rdr.IsDBNull(7) ? (int?)null : rdr.GetInt32(7));                          

                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }        
        public void Insert(PositionTypeInfo posTypeInfo)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@PosTypeName", posTypeInfo.Name);
                dbManager.AddParameters(1, "@PosTypeNameShort", posTypeInfo.NameShort);
                dbManager.AddParameters(2, "@PosTypeEng", posTypeInfo.NameEng);
                dbManager.AddParameters(3, "@PosTypeEngShort", posTypeInfo.NameEngShort);
                dbManager.AddParameters(4, "@EmpTypeSeq", posTypeInfo.EmployeeTypes.Sequence);
                dbManager.AddParameters(5, "@Remarks", posTypeInfo.Remarks);
                dbManager.AddParameters(6, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(7, "@CreatedBy", posTypeInfo.CreatedBy);
                dbManager.AddParameters(8, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_POSITION_TYPES(POS_TYPE_NAME, POS_TYPE_NAME_SHORT"
                        + ", POS_TYPE_ENG, POS_TYPE_ENG_SHORT"
                        + ", EMP_TYPE_SEQ, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@PosTypeName, @PosTypeNameShort, @PosTypeEng, @PosTypeEngShort"
                        + ", @EmpTypeSeq, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(PositionTypeInfo posTypeInfo)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@Seq", posTypeInfo.Sequence);
                dbManager.AddParameters(1, "@PosTypeName", posTypeInfo.Name);
                dbManager.AddParameters(2, "@PosTypeNameShort", posTypeInfo.NameShort);
                dbManager.AddParameters(3, "@PosTypeEng", posTypeInfo.NameEng);
                dbManager.AddParameters(4, "@PosTypeEngShort", posTypeInfo.NameEngShort);
                dbManager.AddParameters(5, "@EmpTypeSeq", posTypeInfo.EmployeeTypes.Sequence);
                dbManager.AddParameters(6, "@Remarks", posTypeInfo.Remarks);
                dbManager.AddParameters(7, "@UpdatedBy", posTypeInfo.UpdatedBy);
                dbManager.AddParameters(8, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_POSITION_TYPES"
                           + " SET POS_TYPE_NAME = @PosTypeName"
                                + ", POS_TYPE_NAME_SHORT = @PosTypeNameShort"
                                + ", POS_TYPE_ENG = @PosTypeEng"
                                + ", POS_TYPE_ENG_SHORT = @PosTypeEngShort"
                                + ", EMP_TYPE_SEQ = @EmpTypeSeq"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE POS_TYPE_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE HRM_MP_POSITION_TYPES"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE POS_TYPE_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }           
    }
}
